//
//  KABCosplayModel.m
//  kabu
//
//  Created by XiangDongMBP on 2017/2/4.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABCosplayModel.h"
#import "NSString+XDLink.h"

@implementation KABCosListItem
- (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; }

+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"cosplayId" : @"id"};
}

- (NSAttributedString *)topContentLinkAStr {
    return [self.top_content xd_matchTopicString];
}
- (NSArray<NSString *> *)topContentTopicArray {
    return [self.top_content xd_topicStringArray];
}
- (NSArray<NSString *> *)topContentTopicAndAtUserArray {
    return [self.top_content xd_topicAndAtUserStringArray];
}
@end

@implementation KABCosplayModel
- (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; }

+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"cosplay_list" : [KABCosListItem class]};
}
- (BOOL)totalLoad {
    return (self.cosplay_list.count >= self.total_rows);
}
- (void)addMoreWithData:(KABCosplayModel *)temp {
    self.total_rows = temp.total_rows;
    [self.cosplay_list addObjectsFromArray:temp.cosplay_list];
}
- (void)removeCosListItem:(KABCosListItem *)item {
    if ([self.cosplay_list indexOfObject:item] != NSNotFound) {
        [self.cosplay_list removeObject:item];
        --self.total_rows;
    }
}
@end



@implementation KABCosDetPubItem
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"pubItemId" : @"id"};
}
@end

@implementation KABCosDetComItem
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"commentId" : @"id"};
}
@end

@implementation KABCosplayDetailVoteInfo
@end
@implementation KABCosplayDetail
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"cosplayId" : @"id"};
}
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"cosplay_detail" : [KABCosDetPubItem class],
             @"comment" : [KABCosDetComItem class]};
}
- (NSAttributedString *)topContentLinkAStr {
    return [self.top_content xd_matchTopicString];
}
- (NSArray<NSString *> *)topContentTopicArray {
    return [self.top_content xd_topicStringArray];
}
- (NSArray<NSString *> *)topContentTopicAndAtUserArray {
    return [self.top_content xd_topicAndAtUserStringArray];
}
- (BOOL)totalLoad {
    return (self.comment.count >= self.total_comment);
}
- (void)addMoreWithData:(KABCosplayDetail *)temp {
    self.total_comment = temp.total_comment;
    [self.comment addObjectsFromArray:temp.comment];
}
- (void)addNewComment:(KABCosDetComItem *)comment {
    if (!comment) {
        return;
    }
    self.total_comment++;
    if (self.comment.count) {
        [self.comment insertObject:comment atIndex:0];
    }else{
        self.comment = [@[comment] mutableCopy];
    }
}
@end



@implementation KABCosPublishItem
- (instancetype)initWithImage:(UIImage *)img originalData:(NSData *)data {
    self = [super init];
    if (self) {
        _img = img;
        _originalData = data;
    }
    return self;
}
// 如果实现了该方法，则处理过程中会忽略该列表内的所有属性
+ (NSArray *)modelPropertyBlacklist {
    return @[@"img", @"originalData"];
}
@end
@implementation KABCosPublishModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"cosplay_detail" : [KABCosPublishItem class]};
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _cosplay_detail = [NSMutableArray array];
        _top_content = @"";
    }
    return self;
}

- (void)removePublishItemAtIndex:(NSInteger)index {
    if (index < self.cosplay_detail.count) {
        [self.cosplay_detail removeObjectAtIndex:index];
    }
}

//- (void)addPublishItemsWithArray:(NSArray<UIImage *> *)array {
//    [array enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        KABCosPublishItem *item = [[KABCosPublishItem alloc] initWithImage:obj];
//        [self.cosplay_detail addObject:item];
//    }];
//}
- (void)addPublishItemsWithImgArray:(NSArray<UIImage *> *)imgArray originalDataArray:(NSArray<NSData *> *)dataArray {
    [imgArray enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSData *originalData = nil;
        if (idx < dataArray.count) {
            originalData = dataArray[idx];
        }
        KABCosPublishItem *item = [[KABCosPublishItem alloc] initWithImage:obj originalData:originalData];
        [self.cosplay_detail addObject:item];
    }];
}
- (void)updateImgUrlWithArray:(NSArray<NSString *> *)array {
    if (array.count != self.cosplay_detail.count) {
        return;
    }
    
    for (NSInteger i = 0; i < self.cosplay_detail.count; ++i) {
        KABCosPublishItem *item = self.cosplay_detail[i];
        item.image_url = array[i];
    }
}
- (void)addTopicWithTitle:(NSString *)title {
    if (self.top_content.length + title.length + 1 > self.topContentMaxLength) {
        return;
    }
    NSMutableString * mStr = [NSMutableString stringWithString:self.top_content];
    [mStr insertString:[NSString stringWithFormat:@"%@#", title] atIndex:self.cursorPostion.location];
    self.top_content = mStr;
}
- (void)addAtUserWithNickname:(NSString *)name {
    if (self.top_content.length + name.length + 1 > self.topContentMaxLength) {
        return;
    }
    NSMutableString * mStr = [NSMutableString stringWithString:self.top_content];
    [mStr insertString:[NSString stringWithFormat:@"%@ ", name] atIndex:self.cursorPostion.location];
    self.top_content = mStr;
}
- (BOOL)isImgUpload {
    if (!self.cosplay_detail.count) {
        return NO;
    }
    for (KABCosPublishItem *item in self.cosplay_detail) {
        if (item.image_url.length == 0) {
            return NO;
        }
    }
    return YES;
}

- (BOOL)isEmpty {
    return (self.top_content.length == 0) && (self.cosplay_detail.count == 0);
}

- (NSArray<UIImage *> *)imgArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.cosplay_detail.count];
    [self.cosplay_detail enumerateObjectsUsingBlock:^(KABCosPublishItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:obj.img];
    }];
    return mArray;
}
- (NSArray<NSData *> *)originalImgArray {
    NSMutableArray<NSData *> *mArray = [NSMutableArray arrayWithCapacity:self.cosplay_detail.count];
    [self.cosplay_detail enumerateObjectsUsingBlock:^(KABCosPublishItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSData *data = obj.originalData ? obj.originalData : UIImageJPEGRepresentation(obj.img, 1);
        if (data) {
            [mArray addObject:data];
        }
    }];
    return mArray;
}
- (NSArray<NSString *> *)urlArray {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:self.cosplay_detail.count];
    [self.cosplay_detail enumerateObjectsUsingBlock:^(KABCosPublishItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mArray addObject:obj.image_url];
    }];
    return mArray;
}

//- (NSObject *)cosplay_detail_json {
//    NSArray<NSMutableDictionary *> *array = [self.cosplay_detail yy_modelToJSONObject];
//    [array enumerateObjectsUsingBlock:^(NSMutableDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        [obj removeObjectForKey:@"img"];
//    }];
//    return array;
//}

@end



@implementation KABCosCelListItem
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper {
    return @{@"uid" : @"id"};
}
@end
@implementation KABCosplayCelebrity
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"celebrities" : [KABCosCelListItem class]};
}
- (BOOL)totalLoad {
    return (self.celebrities.count >= self.total_rows);
}
- (void)addMoreWithData:(KABCosplayCelebrity *)temp {
    self.total_rows = temp.total_rows;
    [self.celebrities addObjectsFromArray:temp.celebrities];
}
@end



@implementation KABCosplayTopicItem
@end
@implementation KABCosplayTopicModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"cosplaytopic_list" : [KABCosplayTopicItem class]};
}
- (BOOL)totalLoad {
    return (self.cosplaytopic_list.count >= self.total_rows);
}
- (void)addMoreWithData:(KABCosplayTopicModel *)temp {
    self.total_rows = temp.total_rows;
    [self.cosplaytopic_list addObjectsFromArray:temp.cosplaytopic_list];
}
@end


@implementation KABCosActivityItem
@end
@implementation KABCosActivityModel
+ (NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass {
    return @{@"coser_list" : [KABCosActivityItem class]};
}
- (BOOL)totalLoad {
    return (self.coser_list.count >= self.total_rows);
}
- (void)addMoreWithData:(KABCosActivityModel *)temp {
    self.total_rows = temp.total_rows;
    [self.coser_list addObjectsFromArray:temp.coser_list];
}
@end
